<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="搜索内容"
        style="width: 20%;margin-right: 10px;"
        class="filter-item"
        clearable
        @keyup.enter.native="searchClick"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="searchClick"
      >搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="addClick"
      >新增
      </el-button>

      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
      >
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="realName" label="真实姓名" />
        <el-table-column prop="name" label="账号" />
        <el-table-column prop="roleName" label="角色" />
        <el-table-column prop="master" label="超级管理员">
          <template slot-scope="scope">
            {{ scope.row.master ? '是':'否' }}
          </template>
        </el-table-column>
        <el-table-column label="启用/禁用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isEnable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="updateEnable(scope.row)"
            />
          </template>

        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="!scope.row.master" type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
            <el-button v-if="!scope.row.master" type="text" size="small" @click="deleteClick(scope.row)">删除
            </el-button>
            <el-button
              v-if="!scope.row.master"
              type="text"
              size="small"
              @click="passwordDialogVisible=true,updatePassword={},updatePassword.id=scope.row.id"
            >修改密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="page.total>0"
        :total="page.total"
        :page.sync="listQuery.current"
        :limit.sync="listQuery.size"
        @pagination="list"
      />
    </div>

    <el-dialog
      title="修改密码"
      :visible.sync="passwordDialogVisible"
      width="30%"
      top="15%"
      center
    >

      <el-input v-model="updatePassword.password" placeholder="请输入新密码" clearable />
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="passwordDialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="changePassword">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog :title="form.id ? '编辑':'新增' " :visible.sync="editDialogTableVisible" @opened="editDialogTableOpened">
      <el-form
        ref="form"
        label-position="left"
        :model="form"
        label-width="100px"
        size="small"
        @keyup.enter.native="save"
      >
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" clearable placeholder="realName" />
        </el-form-item>
        <el-form-item label="账号" prop="name">
          <el-input v-model="form.name" clearable placeholder="name" />
        </el-form-item>
        <el-form-item v-if="!form.id" label="密码">
          <el-input v-model="form.password" clearable placeholder="password" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.roleId" clearable placeholder="请选择角色" style="width: 100%;">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.id" label="创建时间">
          <el-input v-model="form.createTime" disabled="true" />
        </el-form-item>
        <el-form-item v-if="form.id" label="超级管理员">
          <el-input disabled="true" :value="form.master ? '是':'否' " />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="editDialogTableVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="save">保存</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'AdminList',
  components: { Pagination },
  data() {
    return {
      editDialogTableVisible: false,
      passwordDialogVisible: false,
      updatePassword: {
        password: ''
      },
      form: {},
      roleList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      listQuery: {
        name: '',
        current: 1
      },
      page: {
        total: 0
      },
      tableData: []
    }
  },
  created() {
    this.getRoleList()
    this.list()
  },
  methods: {
    list: function() {
      request({
        url: '/api/admin/list',
        method: 'post',
        data: this.listQuery
      }).then(res => {
        this.tableData = res.data.records
        this.page.total = res.data.total
      })
    },
    getRoleList: function() {
      request({
        url: '/api/role/list',
        method: 'post',
        data: { size: 2 << 20 }
      }).then(res => {
        console.log(res.data)
        this.roleList = res.data.records
      })
    },
    editDialogTableOpened: function() {

    },
    changePassword: function() {
      request({
        url: '/api/admin/updatePassword',
        method: 'post',
        data: this.updatePassword
      }).then(res => {
        this.$message({ message: '修改密码成功', type: 'success' })
        this.passwordDialogVisible = false
        this.list()
      })
    },
    updateEnable: function(row) {
      request({
        url: '/api/admin/updateEnable',
        method: 'post',
        data: { id: row.id, isEnable: row.isEnable }
      }).then(res => {
        this.$message({ message: '修改状态成功', type: 'success' })
        this.passwordDialogVisible = false
        this.list()
      })
    },
    addClick: function() {
      this.editDialogTableVisible = true
      this.form = { roleId: null }
    },
    save: function() {
      request({
        url: '/api/admin/save',
        method: 'post',
        data: this.form
      }).then(res => {
        this.$message({ message: '操作成功', type: 'success' })
        this.editDialogTableVisible = false
        this.list()
      })
    },
    editClick: function(row) {
      this.editDialogTableVisible = true
      this.form = this.cloneObject(row)
    },
    deleteClick: function(row) {
      this.$confirm('确定要删除吗？').then(() => {
        request({
          url: '/api/admin/delete',
          method: 'post',
          data: { id: row.id }
        }).then(res => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list()
        })
      })
    },
    searchClick: function() {
      this.listQuery.current = 1
      this.list()
    }

  }
}
</script>

<style scoped>

</style>
